#include "XFtpTask.h"
#include "LogWrapper.h"
#include "XFtpSession.h"
#include <event2/event.h>
#include <event2/bufferevent.h>

void XFtpTask::setCallBack(bufferevent * ev)
{
    bufferevent_setcb(ev, readHandler, writeHandler, eventHandler, this);
    bufferevent_enable(ev, EV_READ | EV_WRITE);
}

void XFtpTask::responseFtpCmd(const std::string & msg)
{
    if (!mSesssion) {
        LOG_ERROR("ftp session is Null");
        return;
    }
    mSesssion->responseFtpCmd(msg);
}

void XFtpTask::connectDataChannel(XFtpTask *task)
{
    if (!mSesssion) {
        LOG_ERROR("ftp session is Null");
        return;
    }
    mSesssion->connectDataChannel(task);
}

void XFtpTask::disconnectDataChannel()
{
    if (!mSesssion) {
        LOG_ERROR("ftp session is Null");
        return;
    }
    mSesssion->disconnectDataChannel();
}

void XFtpTask::sendData(const std::string & data)
{
    if (!mSesssion) {
        LOG_ERROR("ftp session is Null");
        return;
    }
    mSesssion->sendData(data);
}

void XFtpTask::sendData(const char * data, size_t size)
{
    if (!mSesssion) {
        LOG_ERROR("ftp session is Null");
        return;
    }
    mSesssion->sendData(data, size);
}

void XFtpTask::setSession(XFtpSession * session)
{
    mSesssion = session;
}

XFtpTask::XFtpTask()
{
}

XFtpTask::~XFtpTask()
{
}

bool XFtpTask::init()
{
    return true;
}

void XFtpTask::eventHandler(bufferevent * bev, short what, void * arg)
{
    XFtpTask *task = (XFtpTask *)arg;
    task->handleEvent(bev, what);
}

void XFtpTask::readHandler(bufferevent * bev, void * arg)
{
    XFtpTask *task = (XFtpTask *)arg;
    task->read(bev);
}

void XFtpTask::writeHandler(bufferevent * bev, void * arg)
{
    XFtpTask *task = (XFtpTask *)arg;
    task->write(bev);
}
